package cn.hpclub.server.controller.admin;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Business;
import cn.hpclub.server.model.Reward;
import cn.hpclub.server.model.Website;
import cn.hpclub.server.service.RewardService;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.validator.admin.RewardValidator;

public class RewardController extends BaseAdminController<Reward>{

    static String        UI_EDIT                 = "edit";                    // UI编辑标识
    static int           UI_PAGER_NUMBER_DEFAULT = 1;                         // UI默认起始页
    static int           UI_PAGER_SIZE_DEFAULT   = 10;                        // UI页容量
    public static String UI_SESSIONID            = "sid";                     // session
                                                                              // id
    public static String UI_ORG                  = "org";                     // UI列表数据
    public static String UI_BUSI                 = "busi";                    // UI列表数据
    public static String UI_CHANNEL              = "channel";                 // UI列表数据

    public static String HTML_REWARD_LIST        = "/admin/reward_list.tpl";  // 机构管理页
    public static String HTML_REWARD_INPUT       = "/admin/reward_input.tpl"; // 机构编辑页

    public void list(){
        setAttr(Constant.Ui.PAGER,
                RewardService.service.list(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT),
                                           getParaToInt(Constant.Ui.PAGER_SIZE, UI_PAGER_SIZE_DEFAULT),
                                           getSearchInfo()));

        setAttr(UI_ORG, Reward.dao.getOrg());

        setAttr(UI_SESSIONID, getSession().getId());
        this.keepPara("keywords");
        render(HTML_REWARD_LIST);

    }

    public void edit(){

        Reward r = Reward.dao.preview(this.getParaToInt(Reward.ID, 0));

        setAttr(Reward.FIRST_REWARD, r.getBigDecimal(Reward.FIRST_REWARD));
        setAttr(Reward.NORMAL_REWARD, r.getBigDecimal(Reward.NORMAL_REWARD));
        setAttr(Reward.SINGLE_CAP, r.getBigDecimal(Reward.SINGLE_CAP));
        setAttr(Reward.ENABLED, r.getBoolean(Reward.ENABLED));
        setAttr(Reward.ORG_ID, r.getInt(Reward.ORG_ID));
        setAttr(Reward.BUSINESS_ID, r.getInt(Reward.BUSINESS_ID));
        List<String> temp = RewardService.service.getChannel(getParaToInt(Reward.ID, 0));
        setAttr(Reward.CHANNELID + "_temp", temp.get(0));
        setAttr(Reward.CHANNELID, temp.get(1));
        // List<Record> list =
        // RewardService.service.getChannel(getParaToInt(Reward.ID, 0));

        setAttr(UI_ORG, Db.find(InterfaceUtil.getOrganizationLevel1Sql()));
        setAttr(UI_BUSI, Business.dao.getBusiness(false));
        setAttr(UI_CHANNEL, Website.dao.getWebsite(false));
        setAttr(UI_SESSIONID, getSession().getId());
        setAttr(UI_EDIT, getParaToInt(UI_EDIT, 0));
        setAttr(Reward.ID, getParaToInt(Reward.ID, 0));

        // this.keepPara(Organization.dao.COLUMN_ORGANIZATION_ORGTYPE);
        // this.keepPara(Organization.dao.COLUMN_ORGANIZATION_NUMCODE);
        // this.keepPara(Organization.dao.COLUMN_ORGANIZATION_DISPLAYNAME);

        render(HTML_REWARD_INPUT);

    }

    public void add(){

        setAttr(Constant.Ui.PAGER, "");
        setAttr(Reward.FIRST_REWARD, "");
        setAttr(Reward.NORMAL_REWARD, "");
        setAttr(Reward.SINGLE_CAP, "");
        setAttr(Reward.ENABLED, true);
        setAttr(Reward.ORG_ID, 0);
        setAttr(Reward.BUSINESS_ID, 0);
        setAttr(Reward.CHANNELID, "");
        setAttr(UI_ORG, Db.find(InterfaceUtil.getOrganizationLevel1Sql()));
        setAttr(UI_BUSI, Business.dao.getBusiness(false));
        setAttr(UI_CHANNEL, Website.dao.getWebsite(false));

        setAttr(UI_SESSIONID, getSession().getId());
        setAttr(UI_EDIT, getParaToInt(UI_EDIT, 0));
        setAttr(Reward.ID, getParaToInt(Reward.ID, 0));

        // this.keepPara(Organization.dao.COLUMN_ORGANIZATION_ORGTYPE);
        // this.keepPara(Organization.dao.COLUMN_ORGANIZATION_NUMCODE);
        // this.keepPara(Organization.dao.COLUMN_ORGANIZATION_DISPLAYNAME);

        render(HTML_REWARD_INPUT);

    }

    @Before({ RewardValidator.class, Tx.class })
    public void update(){

        if(getPara(Reward.ID, "0").equals("0")){
            Reward.dao.insert(this.getParaMap(), getPara(Reward.CHANNELID).split("/"));
        } else{
            Reward.dao.update(this.getParaMap(), getPara(Reward.CHANNELID).split("/"));
        }

        this.redirect("/reward/list");
    }

    @Before({ RewardValidator.class, Tx.class })
    public void insert(){
        Reward.dao.insert(this.getParaMap(), getPara(Reward.CHANNELID).split("/"));
        this.redirect("/reward/list");
    }

    @Before({ Tx.class })
    public void delete(){
        try{
            if(Reward.dao.delete(getParaValues("ids"))){
                ajaxJsonSuccessMessage("删除成功！");
            } else{
                ajaxJsonErrorMessage("删除失败！");
            }
        }
        catch(Exception e){
            ajaxJsonErrorMessage("删除失败！");
        }
    }

    /**
     * 根据搜索条件获取渠道类型，由reward_input.tpl中的搜索按钮调用
     */
    public void getWebsiteBy(){
        List<Record> website = RewardService.service.getWebsite(false, getPara("searchBy"));
        // System.out.println(website);
        renderJson(website);
    }

}
